<?php
namespace goldcardrsa;

class Rsa{

	private $_privKey;		//私钥
	private $_pubKey;		//公钥
	private $_keyPath;		//密钥保存路径

    public function __construct() {
        $path = __DIR__;
		if(empty($path) || !is_dir($path)) {
			throw new \Exception('Must set the keys save path');   
		}
		$this->_keyPath = $path;
    }

    /**
     * 创建公、私钥
     */
    public function createKey() {
        $r = openssl_pkey_new();
        openssl_pkey_export($r, $privKey);
        file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR . 'private_key.pem', $privKey);
        $this->_privKey = openssl_pkey_get_public($privKey);
        $rp = openssl_pkey_get_details($r);
        $pubKey = $rp['key'];
        file_put_contents($this->_keyPath . DIRECTORY_SEPARATOR .  'public_key.pem', $pubKey);
        $this->_pubKey = openssl_pkey_get_public($pubKey);
    }
    
    /**
     * 设置私钥
     */
    public function setupPrivKey() {
        if(is_resource($this->_privKey)){
            return true;
        }
        $file = $this->_keyPath . DIRECTORY_SEPARATOR . 'private_key.pem';
        $prk = file_get_contents($file);
        $this->_privKey = openssl_pkey_get_private($prk);
        return true;
    }
    
    /**
     * 设置公钥
     */
    public function setupPubKey() {
        if(is_resource($this->_pubKey)){
            return true;
        }
        $file = $this->_keyPath . DIRECTORY_SEPARATOR .  'public_key.pem';
        $puk = file_get_contents($file);
        $this->_pubKey = openssl_pkey_get_public($puk);
        return true;
    }
    
    /**
     * 私钥加密
     */
    public function privEncrypt($data) {
        if(!is_string($data)) {
            return null;
        }
        $this->setupPrivKey();
    
        $crypto = '';
        foreach (str_split($data, 117) as $chunk) {
            openssl_private_encrypt($chunk, $data, $this->_privKey);
            $crypto .= $data;
        }
        return base64_encode($crypto);
    }
    
    /**
     * 私钥解密
     */
    function privDecrypt($crypted) {
        if(!is_string($crypted)) {
            return null;
        }
        $this->setupPrivKey();
    
        $crypto = '';
        foreach (str_split(base64_decode($crypted), 128) as $chunk)
        {
            openssl_private_decrypt($chunk, $decryptData, $this->_privKey);
            $crypto .= $decryptData;
        }
        return $crypto;
    }
    
    /**
     * 公钥加密
     */
    function pubEncrypt($data) {
        if(!is_string($data)) {
            return null;
        }
        $this->setupPubKey();
    
        $crypto = '';
        foreach (str_split($data, 117) as $chunk)
        {
            openssl_public_encrypt($chunk, $encryptData, $this->_pubKey);
            $crypto .= $encryptData;
        }
        return base64_encode($crypto);
    }
    
    /**
     * 公钥解密
     */
    public function pubDecrypt($crypted) {
        if(!is_string($crypted)) {
            return null;
        }
        $this->setupPrivKey();
    
        $crypto = '';
        foreach (str_split(base64_decode($crypted), 128) as $chunk)
        {
            openssl_public_decrypt($chunk, $crypted, $this->_pubKey);
            $crypto .= $crypted;
        }
        return $crypto;
    }
    
    /**
     * 关闭
     */
    public function __destruct() {
        unset($this->_privKey);
        unset($this->_pubKey);
        //@fclose($this->_privKey);
        //@fclose($this->_pubKey);
    }
}
